package wow.data
{
	import mx.collections.ArrayList;
	
	import wow.Datas;

	public class Unit
	{
		[Bindable] public var name:String;
		[Bindable] public var clazz:int;
		[Bindable] public var spec:String;
		[Bindable] public var maxLife:int;
		[Bindable] public var life:int;
		[Bindable] public var buffs:ArrayList;
		[Bindable] public var debuffs:ArrayList;
		[Bindable] public var pret:Boolean;
		[Bindable] public var active:Boolean;
		[Bindable] public var activeSpell:Spell;
		[Bindable] public var activeDamage:int;
		[Bindable] public var activeHeal:int;
		
		public function Unit(name:String, clazz:int, spec:String, maxLife:int)
		{
			this.name = name;
			this.clazz = clazz;
			this.spec = spec;
			this.maxLife = maxLife;
			this.life = maxLife;
			this.buffs = new ArrayList();
			this.debuffs = new ArrayList();
			this.pret = true;
			this.active = false;
			this.activeSpell = null;
		}
		
		/** Indique si l'unité a le buff */
		public function hasBuff(buffid:int):Boolean
		{
			for (var index:int; index<buffs.length; index++)
			{
				var buff:Buff = buffs.getItemAt(index) as Buff;
				if (buff.spell.id == buffid)
				{
					return true;
				}
			}
			
			return false;
		}
		
		/** Indique si l'unité a le buff */
		public function getBuff(buffid:int):Buff
		{
			for (var index:int; index<buffs.length; index++)
			{
				var buff:Buff = buffs.getItemAt(index) as Buff;
				if (buff.spell.id == buffid)
				{
					return buff;
				}
			}
			
			return null;
		}
		
		/** Indique si l'unité a le debuff */
		public function getDebuff(debuffid:int):Buff
		{
			for (var index:int; index<buffs.length; index++)
			{
				var debuff:Buff = debuffs.getItemAt(index) as Buff;
				if (debuff.spell.id == debuffid)
				{
					return debuff;
				}
			}
			
			return null;
		}
		
		public function removeBuffById(buffid:int):void
		{
			for (var index:int; index<buffs.length; index++)
			{
				var buff:Buff = buffs.getItemAt(index) as Buff;
				if (buff.spell.id == buffid)
				{
					if (buff.spell.spell_type == Datas.BUFF_STACK)
					{
						buff.reminder--;
						if (buff.reminder == 0)
						{
							this.buffs.removeItem(buff);
						}
					}
					else
					{
						this.buffs.removeItem(buff);
					}
				}
			}
		}
		
		public function removeDebuffById(debuffid:int):void
		{
			for (var index:int; index<debuffs.length; index++)
			{
				var debuff:Buff = debuffs.getItemAt(index) as Buff;
				if (debuff.spell.id == debuffid)
				{
					this.debuffs.removeItem(debuff);
				}
			}
		}
		
		public function removeBuff(buff:Buff):void
		{
			this.buffs.removeItem(buff);
		}
		
		public function removeDebuff(debuff:Buff):void
		{
			this.debuffs.removeItem(debuff);
		}
	}
}